package com.cxs.musicplaywidget.player.store;

import com.cxs.musicplaywidget.ResourceTable;
import com.cxs.musicplaywidget.util.LogUtils;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;

/**
 * @ClassName ImageStore
 * @Author cxs
 * @Version 1.0
 * @Description TODO
 * @Date 2022/04/04
 */
public class ImageStore {
    private static final String tag = ImageStore.class.getSimpleName();
    private static final int CD_RES_ID = ResourceTable.Media_cd;
    private static ImageStore instance = null;
    private final Context context;
    private static ImageSource imageSource = null;

    private ImageStore(Context context) {
        this.context = context;
        imageSource = getImageSource();
    }

    public static ImageStore getInstance(Context context) {
        if (instance == null) {
            synchronized (ImageStore.class) {
                if (instance == null) {
                    instance = new ImageStore(context);
                }
            }
        }
        return instance;
    }

    public PixelMap getRotatePixelMap(float angle) {
        if (imageSource == null) {
            imageSource = getImageSource();
        }
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        // 以图像中心点旋转
        decodingOptions.rotateDegrees = angle;
//            imageSource.createThumbnailPixelmap()
        // 从图像数据源解码并创建PixelMap图像。
        PixelMap pixelMap = instance.imageSource.createPixelmap(decodingOptions);
        return pixelMap;
    }

    public byte[] getRotateImage(float angle) {
        if (imageSource == null) {
            imageSource = getImageSource();
        }
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        // 以图像中心点旋转
        decodingOptions.rotateDegrees = angle;
//            imageSource.createThumbnailPixelmap()
        // 从图像数据源解码并创建PixelMap图像。
        PixelMap pixelMap = instance.imageSource.createPixelmap(null);
//        int len = (int) pixelMap.getPixelBytesCapacity();
//        // 创建指定容量的ByteBuffer对象
//        ByteBuffer byteBuffer = ByteBuffer.allocate(len);
        Size size = pixelMap.getImageInfo().size;
        ByteBuffer byteBuffer = ByteBuffer.allocate(size.width * size.height * 4);
        // 从pixelMap中读取ByteBuffer
        pixelMap.readPixels(byteBuffer);
        byteBuffer.flip();
        return byteBuffer.array();
    }

    /**
     * 创建图像数据源 ImageSource对象
     * @return
     */
    private ImageSource getImageSource() {
        ImageSource imageSource;
        InputStream inputStream = null;
        try {
            inputStream = context.getResourceManager().getResource(CD_RES_ID);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpg";
            imageSource = ImageSource.create(inputStream, sourceOptions);
        } catch (IOException | NotExistException e) {
            imageSource = null;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    LogUtils.debug(tag, "IOException " + e.getMessage());
                }
            }
        }
        return imageSource;
    }
}
